args <- commandArgs(TRUE)
#snp<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/SNP.txt",sep="\t",head=T)
snp<-read.table(args[4],sep="\t",head=T)
#rownames(snp)<-as.character(snp[,1])
#snp<-snp[,2:ncol(snp)]
gene<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/GE.txt",sep="\t",head=T)
k1<-c("gene","snp","Intercept_Estimate","snp_Estimate","Intercept_Std.Error","snp_Std.Error","Intercept_t value","snp_t value","Intercept_Pr(>|t|)","snp_Pr(>|t|)")
data<-read.table(args[1],sep="\t",head=T)
colnames(data)<-c("rsid","geneid")

#sorting accordind to file order
sort_order<-read.table(args[3],head=T)
sort_order<-as.vector(t(sort_order))
gene<-gene[,sort_order]
rownames(gene)<-gene$geneid


data<-merge(data,snp,by="rsid")
data<-merge(data,gene,by="geneid")
 
#gene<-gene[,2:ncol(gene)]
zz <- file(args[2],"w")
writeLines(paste(k1,collapse=" "),con=zz,sep="\n")
#glmfun<-function(x)
#{
#	glm1 <- lm(x[3:209] ~ x[210:416], family=gaussian(link="log"))
#	kk<-c(as.character(x[1]),as.character(x[2]),as.vector(coefficients(summary(glm1))))
#	print(kk)
#}
i<-1
while(i<nrow(data1))
{
        print(i)
	g2<-as.numeric(data1[i,3:209])
        g1<-as.numeric(data1[i,210:416])
	kk<-c()
	kk<-c(as.character(data1[i,2]),as.character(data1[i,1]),as.vector(coefficients(summary(lm(g1 ~ g2, family=gaussian(link="log"))))))
	writeLines(paste(kk,collapse=" "),con=zz,sep="\n")
	i<-i+1
}
#}
#apply(data1,1,glmfun)
close(zz)








pgenid<-0
colnm_snp<-colnames(snp)
colnm_snp[1]<-"geneid"
colnames(snp)<-colnm_snp
snp<-snp[,colnames(gene)]
nrow_snp<-nrow(snp)
nrow_gene<-nrow(gene)
overall<-rbind(as.matrix(snp),as.matrix(gene))
snp<-c()
gene<-c()
overall<-t(overall)
colnames(overall)<-overall[1,]
overall<-overall[2:nrow(overall),]
#colnames(overall)<-overall[1,]
#overall1<-as.numeric(as.matrix( overall))
overall1<- apply(overall,2,as.numeric)
#colnames(overall1)<-colnames(overall)
rownames(overall1)<-rownames(overall)
colname_ov<-colnames(overall)
i<-1
while(i<nrow_snp+nrow_gene+1)
{
	if(i>nrow_snp)
	{
		colname_ov[i]<-paste("g",colname_ov[i],sep="_")
	}
	i<-i+1
}
colnames(overall1)<-colname_ov
overall1<-as.data.frame(overall1)
overall<-c()
i<-1
while(i<nrow(data))
{
	print(i)
	##g2<-as.numeric(t(as.matrix(snp[data[i,1],2:ncol(snp)])))
	#g1<-as.numeric(t(as.matrix(gene[which(gene[,1] ==data[i,2]),2:ncol(snp)])))
	#if(pgenid != data[i,2])
	#{	
		#print(data[i,2])
	##	g1<-as.numeric(t(as.matrix(gene[as.character(data[i,2]),2:ncol(snp)])))
	#}
	#if(ncol(g2) > 1 & ncol(g1) > 1)
	#{
	#d1 <- data.frame(g2,g1)
	#glm1 <- glm(g1 ~ g2, data=d1,family=gaussian(link="log"))
	##glm1 <- glm(g1 ~ g2, family=gaussian(link="log"))
	#K<-parse(text=paste(paste("g",data[i,2],sep="_"),data[i,1],sep='~'))
	K<-parse(text=paste(paste("overall1",paste("g",data[i,2],sep="_"),sep='$'),paste("overall1",data[i,1],sep='$'),sep='~'))
	#glm1 <- glm(eval(K), data=overall1,family=gaussian(link="log"))
	#glm1 <- glm(eval(K),family=gaussian(link="log"))
	glm1 <- lm(eval(K))
	kk<-c( as.character(data[i,2]),as.character(data[i,1]),as.vector(coefficients(summary(glm1))))
	#print(kk)
	k1<-rbind(k1,kk)
	kk<-c()	
	#}
	i<-i+1
	#pgenid<-data[i,2]	
}
write.table(k1,args[2],quote=FALSE,sep="\t",col.names=FALSE,row.names=FALSE)
